package com.zhengxyou.jetpackdemo.navigation;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.zhengxyou.jetpackdemo.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

/**
 * https://blog.csdn.net/cekiasoo/article/details/80739805
 * https://blog.csdn.net/c10WTiybQ1Ye3/article/details/80912603
 */
public class NavigationActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);

        Toolbar toolbar = findViewById(R.id.toolbar);
        CollapsingToolbarLayout toolbarLayout = findViewById(R.id.toolbarLayout);
//        setSupportActionBar(toolbar);

        FragmentManager manager = getSupportFragmentManager();
        NavHostFragment hostFragment = (NavHostFragment) manager.findFragmentById(R.id.nav_host_fragment);
        NavController navController = hostFragment.getNavController();
        NavigationUI.setupActionBarWithNavController(NavigationActivity.this, navController);
        AppBarConfiguration configuration = new AppBarConfiguration.Builder(R.id.nav_graph).build();

        NavigationUI.setupWithNavController(toolbar, navController, configuration);

//        NavGraph graph=hostFragment.getNavController().getNavInflater().inflate(R.navigation.nav_graph);
//        NavDestination destination=graph.findNode(R.id.blankFragment);
//        BlankFragmentArgs blankFragmentArgs=new BlankFragmentArgs.Builder("156").build();


        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
                Log.e("TAG", destination.getLabel().toString());
            }
        });

        //编程方式创建NavHostFragment具有特定图形资源的资源
//        NavHostFragment finalHost = NavHostFragment.create(R.navigation.nav_graph);
//        getSupportFragmentManager()
//                .beginTransaction()
//                .replace(R.id.nav_host, finalHost)
//                .setPrimaryNavigationFragment(finalHost)// this is the equivalent to app:defaultNavHost="true"
//                .commit();
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, NavigationActivity.class);
        context.startActivity(starter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    /**
     * 确保NavHostFragment 拦截系统后退按钮
     * 还可以通过设置app:defaultNavHost="true"属性实现
     */
    @Override
    public boolean onSupportNavigateUp() {
        return Navigation.findNavController(this, R.id.nav_host_fragment).navigateUp();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
